package cn.iocoder.yudao.module.hs.service.rfidchangerecord;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hs.controller.admin.rfidchangerecord.vo.RfidChangeRecordPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.rfidchangerecord.vo.RfidChangeRecordSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.picktask.PickTaskDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.rfid.RfidDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.rfidchangerecord.RfidChangeRecordDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.tray.TrayDO;
import cn.iocoder.yudao.module.hs.dal.mysql.picktask.PickTaskMapper;
import cn.iocoder.yudao.module.hs.dal.mysql.rfidchangerecord.RfidChangeRecordMapper;
import cn.iocoder.yudao.module.hs.enums.picktask.PickTaskStatusEnum;
import cn.iocoder.yudao.module.hs.service.rfid.RfidService;
import cn.iocoder.yudao.module.hs.service.tray.TrayService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.*;

/**
 * RFID更换记录 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class RfidChangeRecordServiceImpl implements RfidChangeRecordService {

    @Resource
    private RfidChangeRecordMapper rfidChangeRecordMapper;
    @Resource
    @Lazy
    private RfidService rfidService;
    @Resource
    private TrayService trayService;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private PickTaskMapper pickTaskMapper;

    @Override
    @Transactional
    public Long createRfidChangeRecord(RfidChangeRecordSaveReqVO createReqVO) {
        // 插入更换记录
        RfidChangeRecordDO rfidChangeRecord = BeanUtils.toBean(createReqVO, RfidChangeRecordDO.class);
        RfidDO oldRfid = rfidService.getRfid(rfidChangeRecord.getOldRfidCode());
        // 校验旧RFID是否存在
        if (oldRfid == null) {
            throw exception(RFID_NOT_EXISTS);
        }
        RfidDO newRfid = rfidService.getRfid(rfidChangeRecord.getNewRfidCode());
        // 校验新RFID是否存在
        if (newRfid == null) {
            throw exception(RFID_NOT_EXISTS);
        }
        // 如果仓库ID为空,则获取旧RFID的仓库ID
        if (rfidChangeRecord.getWarehouseId() == null) {
            TrayDO tray = trayService.getTray(newRfid.getTrayCode());
            if (tray == null) {
                throw exception(TRAY_NOT_EXISTS);
            }
            rfidChangeRecord.setWarehouseId(tray.getWarehouseId());
        }
        if (StrUtil.isEmpty(rfidChangeRecord.getChangeReason())) {
            rfidChangeRecord.setChangeReason("OTHER");
        }
        rfidChangeRecord.setChangeStatus(1);
        rfidChangeRecord.setChangeTime(LocalDateTime.now());
        Long loginUserId = getLoginUserId();
        AdminUserRespDTO user = adminUserApi.getUser(loginUserId);
        rfidChangeRecord.setChangeUserId(loginUserId);
        rfidChangeRecord.setChangeUserName(user.getNickname());
        // 插入更换记录
        rfidChangeRecordMapper.insert(rfidChangeRecord);

        List<PickTaskDO> pickTaskDOS = pickTaskMapper.selectList(PickTaskDO::getRfidCode, oldRfid.getCode());
        if (CollUtil.isNotEmpty(pickTaskDOS)) {
            PickTaskDO pickTaskDO = pickTaskDOS.get(0);
            pickTaskDO.setStatus(PickTaskStatusEnum.PICK_OVER.getStatus());
            pickTaskMapper.updateById(pickTaskDO);
        }
        // 返回
        return rfidChangeRecord.getId();
    }

    @Override
    public void updateRfidChangeRecord(RfidChangeRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateRfidChangeRecordExists(updateReqVO.getId());
        // 更新
        RfidChangeRecordDO updateObj = BeanUtils.toBean(updateReqVO, RfidChangeRecordDO.class);
        // 更新记录
        rfidChangeRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRfidChangeRecord(Long id) {
        // 校验存在
        validateRfidChangeRecordExists(id);
        // 删除
        rfidChangeRecordMapper.deleteById(id);
    }

    private void validateRfidChangeRecordExists(Long id) {
        if (rfidChangeRecordMapper.selectById(id) == null) {
            throw exception(RFID_CHANGE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RfidChangeRecordDO getRfidChangeRecord(Long id) {
        return rfidChangeRecordMapper.selectById(id);
    }

    @Override
    public PageResult<RfidChangeRecordDO> getRfidChangeRecordPage(RfidChangeRecordPageReqVO pageReqVO) {
        return rfidChangeRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public void insertBatch(List<RfidChangeRecordSaveReqVO> rfidChangeRecordSaveReqVOS) {
        List<RfidChangeRecordDO> rfidChangeRecordDOList = BeanUtils.toBean(rfidChangeRecordSaveReqVOS, RfidChangeRecordDO.class);
        rfidChangeRecordMapper.insertBatch(rfidChangeRecordDOList);
    }
}